#include<stdio.h>
#include <string.h>
#include <stdlib.h>
struct goods
{    	  	  	   
    int num;
    char type[20];
    char name[20];
    int  counts;
    struct goods*next;
}    	  	  	   *head; //1
//函数声明
void creat();
void show();
int  updateCount(int);
void Pfree(struct goods *head);
int main()   
{    	  	  	   
    int num;
    creat();    //1
    show();     //1
    printf("请输入要修改商品的编号：\n");
    scanf("%d", &num); //1
    if(updateCount(num)==1) //2
    {    	  	  	   
    printf("修改后的商品：\n");
    show(); //1
    }
    Pfree(head);     //1
    return 0;
}    	  	  	   
void  creat()   //2
{    	  	  	   
    struct goods *q, *tail;    //1
    char flag = 'Y';
    head = (struct goods *)malloc(sizeof(struct goods)); 
    head->next = NULL; //1
    tail = head;            //1
    while (flag == 'Y')
    {    	  	  	   
        printf("请依次输入每个商品编号、类型、名称、数量：\n");
        q = (struct goods *)malloc(sizeof(struct goods));     //1
        q->next = NULL;
        scanf("%d %s %s %d", &q->num, &q->type, &q->name, &q->counts);  //1
        tail->next = q;  //1
        tail = q;   //1
        printf("是否继续输入,按Y键继续输入，其他键就结束.\n");
        getchar();
        flag = getchar();    //1
    }
}    	  	  	   
void show() //2
{    	  	  	   
    struct goods *out;   //1
    printf("输出所有商品信息为：编号 类型 名称 数量:\n");
    
    out = head->next;  
    while (out)        //1
    {    	  	  	   
        printf("%d %s %s %d\n", out->num, out->type, out->name, out->counts);//1
        out = out->next;     //1
    }
}    	  	  	   
//更新商品的信息
int updateCount(int num)   //2
{    	  	  	   
    struct goods *p;   //1
    int flag = 0;
    char  mark;
    mark = 'f';                              
    p = head->next;        //1                   
    while (p != NULL)     //1
    {    	  	  	   
        if (p->num == num)          //1     
        {    	  	  	   
            printf("请重新输入该商品的数量 ：\n");
            scanf("%d", &p->counts);//1
            flag = 1;
            break;
        }
        else
            p = p->next; //1
    }
    
    if (flag == 0)    //1                             
    {    	  	  	   
        printf("修改失败.\n");
        printf("库存中不存在该商品.\n");
        return 0;
    }
    return  flag;//1
}    	  	  	   
  
void Pfree(struct goods *head)//2
{    	  	  	   
   struct goods *p, *q; //1
    p = head;
    while (p->next != NULL) //1
    {    	  	  	   
        q = p->next;
        p->next = q->next;//1
        free(q);//1
    }
    free(p);
}